package com.gold.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gold.entity.Exchange;
import com.gold.mapper.ExchangeMapper;
import com.gold.service.ExchangeService;

@Service
public class ExchangeServiceImpl implements ExchangeService {

	@Autowired
	private ExchangeMapper em;

	public void deleteExchange(String[] exchangIdString) {
		for (int i = 0; i < exchangIdString.length; i++) {
			em.deleteExchange(Integer.parseInt(exchangIdString[i]));
		}
	}

	public void insertExchange(Exchange exchange) {
		em.insertExchange(exchange);
	}

	public List<Exchange> selectAllExchange() {
		return em.selectAllExchange();
	}

	public List<Exchange> selectAllExchangeByPage(Map<String, String> map) {
		int pageSize = 5;
		int userCount = em.selectAllExchange().size();
		int pageCount = userCount % pageSize == 0 ? userCount / pageSize : userCount / pageSize + 1;
		String nowPageString = map.get("nowPageString");
		if (nowPageString == null) {
			nowPageString = "1";
		}
		int nowPage = Integer.parseInt(nowPageString);
		if (nowPage <= 0) {
			nowPage = pageCount;
		}
		if (nowPage > pageCount) {
			nowPage = 1;
		}
		map.put("start", "" + ((nowPage - 1) * pageSize + 1));
		map.put("end", "" + (nowPage * pageSize));
		map.put("nowPageString", nowPage + "");
		return em.selectAllExchangeByPage(map);
	}

	public List<Exchange> selectExchangeByProvId(int exchangeProvId) {
		return em.selectExchangeByProvId(exchangeProvId);
	}

	public void updateExchange(Exchange exchange) {
		em.updateExchange(exchange);
	}

	public Exchange selectExchangeByExchangeId(int exchangeId) {
		return em.selectExchangeByExchangeId(exchangeId);
	}

}
